/**
 * Copyright (c) 2013, Robert Cherry * All rights reserved.
 *
 * This file is part of the Faust Editor.
 *
 * The Faust Editor is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * The Faust Editor is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * The Faust Editor. If not, see <http://www.gnu.org/licenses/>.
 */
package form;

import io.resource.ResourceDelegate;
import io.resource.ResourceReader;
import java.awt.Dimension;
import javax.swing.Box.Filler;
import javax.swing.BoxLayout;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import tools.WorldCanvasTool;

/**
 *
 * @author robert
 */
public class FileToolbar extends javax.swing.JPanel {

    //Variable Declaration
    // Project Classes
    private FaustEditor editor;
    private ResourceDelegate delegate;
    private WorldCanvas canvas;
    // End of Variable Delcaration

    public FileToolbar(FaustEditor editor, ResourceDelegate delegate, WorldCanvas canvas) {

        //
        initComponents();

        //
        this.editor = editor;
        this.delegate = delegate;
        this.canvas = canvas;

        //
        init();
    }

    private void init() {

        try {

            // Grab source folder
            final Class closs = getClass();

            loadJButton.setIcon(ResourceReader.readClassPathIcon(closs,"/icons/icon-open24.png"));
            saveJButton.setIcon(ResourceReader.readClassPathIcon(closs,"/icons/icon-save24.png"));
            changeJButton.setIcon(ResourceReader.readClassPathIcon(closs,"/icons/icon-queue24.png"));
            selectorJButton.setIcon(ResourceReader.readClassPathIcon(closs,"/icons/icon-selector24.png"));
            markerJButton.setIcon(ResourceReader.readClassPathIcon(closs,"/icons/icon-marker24.png"));
            eraserJButton.setIcon(ResourceReader.readClassPathIcon(closs,"/icons/icon-eraser24.png"));
        } catch (NullPointerException npe) {
            //
        }

        //
        saveJButton.setEnabled(false);

        //
        final Dimension tripleComponentDimension = new Dimension(106, 32);
        final Dimension doubleComponentDimension = new Dimension(72, 32);
        final Dimension singleComponentDimension = new Dimension(40, 32);
        final Dimension strutDimension = new Dimension(4, 32);

        //
        final JToolBar fileBar = new JToolBar();
        fileBar.setFloatable(false);
        fileBar.setOrientation(JToolBar.HORIZONTAL);
        fileBar.setPreferredSize(tripleComponentDimension);
        fileBar.setMaximumSize(tripleComponentDimension);
        fileBar.setMinimumSize(tripleComponentDimension);
        fileBar.add(new Filler(strutDimension, strutDimension, strutDimension));
        fileBar.add(loadJButton);
        fileBar.add(saveJButton);
        fileBar.add(changeJButton);
        fileBar.add(new Filler(strutDimension, strutDimension, strutDimension));

        //
        final JToolBar toolBar = new JToolBar();
        toolBar.setFloatable(false);
        toolBar.setOrientation(JToolBar.HORIZONTAL);
        toolBar.setPreferredSize(tripleComponentDimension);
        toolBar.setMaximumSize(tripleComponentDimension);
        toolBar.setMinimumSize(tripleComponentDimension);
        toolBar.add(new Filler(strutDimension, strutDimension, strutDimension));
        toolBar.add(selectorJButton);
        toolBar.add(markerJButton);
        toolBar.add(eraserJButton);
        toolBar.add(new Filler(strutDimension, strutDimension, strutDimension));

        // Change the size of the panel
        setPreferredSize(new Dimension(216, 32));
        setMaximumSize(new Dimension(216, 32));

        //
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

        //
        add(fileBar);
        add(new JSeparator(JSeparator.VERTICAL));
        add(toolBar);
        add(new JSeparator(JSeparator.VERTICAL));
    }

    public void setInvalidated() {

        // Enable Save button
        saveJButton.setEnabled(true);
    }

    public void setValidated() {

        // Disable save button
        saveJButton.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loadJButton = new javax.swing.JButton();
        saveJButton = new javax.swing.JButton();
        selectorJButton = new javax.swing.JButton();
        markerJButton = new javax.swing.JButton();
        eraserJButton = new javax.swing.JButton();
        changeJButton = new javax.swing.JButton();

        loadJButton.setToolTipText("Load an Existing Package");
        loadJButton.setFocusable(false);
        loadJButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        loadJButton.setMaximumSize(new java.awt.Dimension(32, 32));
        loadJButton.setMinimumSize(new java.awt.Dimension(32, 32));
        loadJButton.setPreferredSize(new java.awt.Dimension(32, 32));
        loadJButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        loadJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadJButtonActionPerformed(evt);
            }
        });

        saveJButton.setToolTipText("Save Changes");
        saveJButton.setFocusable(false);
        saveJButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveJButton.setMaximumSize(new java.awt.Dimension(32, 32));
        saveJButton.setMinimumSize(new java.awt.Dimension(32, 32));
        saveJButton.setPreferredSize(new java.awt.Dimension(32, 32));
        saveJButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        saveJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveJButtonActionPerformed(evt);
            }
        });

        selectorJButton.setToolTipText("Marker Select Mode");
        selectorJButton.setFocusable(false);
        selectorJButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        selectorJButton.setMaximumSize(new java.awt.Dimension(32, 32));
        selectorJButton.setMinimumSize(new java.awt.Dimension(32, 32));
        selectorJButton.setPreferredSize(new java.awt.Dimension(32, 32));
        selectorJButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        selectorJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectorJButtonActionPerformed(evt);
            }
        });

        markerJButton.setToolTipText("Marker Add Mode");
        markerJButton.setFocusable(false);
        markerJButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        markerJButton.setMaximumSize(new java.awt.Dimension(32, 32));
        markerJButton.setMinimumSize(new java.awt.Dimension(32, 32));
        markerJButton.setPreferredSize(new java.awt.Dimension(32, 32));
        markerJButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        markerJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                markerJButtonActionPerformed(evt);
            }
        });

        eraserJButton.setToolTipText("Marker Remove Mode");
        eraserJButton.setFocusable(false);
        eraserJButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        eraserJButton.setMaximumSize(new java.awt.Dimension(32, 32));
        eraserJButton.setMinimumSize(new java.awt.Dimension(32, 32));
        eraserJButton.setPreferredSize(new java.awt.Dimension(32, 32));
        eraserJButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        eraserJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eraserJButtonActionPerformed(evt);
            }
        });

        changeJButton.setToolTipText("View Changes");
        changeJButton.setFocusable(false);
        changeJButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        changeJButton.setMaximumSize(new java.awt.Dimension(32, 32));
        changeJButton.setMinimumSize(new java.awt.Dimension(32, 32));
        changeJButton.setPreferredSize(new java.awt.Dimension(32, 32));
        changeJButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        changeJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeJButtonActionPerformed(evt);
            }
        });

        setMaximumSize(new java.awt.Dimension(32, 32));
        setMinimumSize(new java.awt.Dimension(32, 32));
        setPreferredSize(new java.awt.Dimension(32, 32));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 32, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 32, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void loadJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadJButtonActionPerformed

        //
        if (delegate.getScanMode() == ResourceDelegate.SCAN_BIASED) {

            // open a resource dataPackage
            editor.open();
        } else if (delegate.getScanMode() == ResourceDelegate.SCAN_UNBIASED) {

            // Flush
            delegate.flush();

            // Add all files unbiased
            delegate.performScan(editor.getDataDirectory(), true, editor.acceptsDataPackages());
            
            //
            delegate.validate();
        }
    }//GEN-LAST:event_loadJButtonActionPerformed

    private void saveJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveJButtonActionPerformed

        // save changes to resource plugins that have been affected
        editor.save();

        // Validated
        setValidated();
    }//GEN-LAST:event_saveJButtonActionPerformed

    private void selectorJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectorJButtonActionPerformed

        // Grab the world canvas and change the mode of the map tool
        final WorldCanvasTool tool = canvas.getTool();

        // Must exist
        if (tool != null) {

            // Change to mode
            tool.setMode(WorldCanvasTool.FLAG_SELECT);
        }
    }//GEN-LAST:event_selectorJButtonActionPerformed

    private void markerJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_markerJButtonActionPerformed

        // Grab the world canvas and change the mode of the map tool
        final WorldCanvasTool tool = canvas.getTool();

        // Must exist
        if (tool != null) {

            // Change to mode
            tool.setMode(WorldCanvasTool.FLAG_MARKER);
        }
    }//GEN-LAST:event_markerJButtonActionPerformed

    private void eraserJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eraserJButtonActionPerformed

        // Grab the world canvas and change the mode of the map tool
        final WorldCanvasTool tool = canvas.getTool();

        // Must exist
        if (tool != null) {

            // Change to mode
            tool.setMode(WorldCanvasTool.FLAG_ERASER);
        }
    }//GEN-LAST:event_eraserJButtonActionPerformed

    private void changeJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeJButtonActionPerformed
        
        // TODO add your handling code here:
        final ChangeMonitor monitor = new ChangeMonitor(editor, delegate, true);
        monitor.setLocationRelativeTo(editor);
        monitor.setVisible(true);
        
        // Dispose of the monitor
        monitor.dispose();
    }//GEN-LAST:event_changeJButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton changeJButton;
    private javax.swing.JButton eraserJButton;
    private javax.swing.JButton loadJButton;
    private javax.swing.JButton markerJButton;
    private javax.swing.JButton saveJButton;
    private javax.swing.JButton selectorJButton;
    // End of variables declaration//GEN-END:variables
}
